from layer import *
from synapses import *
import synapses

class CNet(object):
    
    def __init__(self, empty,  fixSynapses=[],  nInputs=0,  structure=[]):
        
        if not empty:
            nInputs = len(fixSynapses[0])
            for numLayers in range(0,  len(fixSynapses)):
                structure.append(len(fixSynapses[numLayers][0]))
        
        self.alpha = 0.5
        #synapses, will be a list of synapsis
        self.synapses = []
        self.inc_synapses = []
        #Dic of layers
        self.layers ={}
        #init layers
        for layer in range(0,  len(structure)):
            self.synapses.append(CSynapsis())
            self.inc_synapses.append(CSynapsis())
            if layer == 0:
                #Input layer
                self.layers["inputLayer"] = CLayer()
                if empty: #Configure random values
                    self.synapses[layer].shakeMatrix(nInputs,  structure[layer])
                else: #Load fixed Values
                    self.synapses[layer].matrix = fixSynapses[layer]
            else:
                if layer == (len(structure)-1):
                        #Output layer
                    self.layers["outputLayer"] = CLayer()
                    if empty:
                        self.synapses[layer].shakeMatrix(structure[layer-1]+1,  structure[layer])
                    else:
                        self.synapses[layer].matrix = fixSynapses[layer]
                else:
                    #Hidden Layers
                    for hLayer in range(1, len(structure)-1):
                        self.layers["hiddenLayer"+str(hLayer)] = CLayer()
                        if empty:
                            self.synapses[layer].shakeMatrix(structure[layer-1]+1,  structure[layer])
                        else: 
                            self.synapses[layer].matrix = fixSynapses[layer]
                                
    def printSynapses(self):
        for syn in range(0,  len(self.synapses)):
            print self.synapses[syn].matrix
        
    def propagate(self, input):        
        count = 0
        for layer in self.layers.keys():           
            self.layers[layer].inputs = []
            if layer == "inputLayer":
                #self.layers[0] first layer
                self.layers[layer].inputs = input
                lastLayer = layer
            else:
                #self.layers[1:] Other layers
                self.layers[layer].inputs.append(1.0)
                self.layers[layer].inputs[1:] = self.layers[lastLayer].values
                lastLayer = layer
            #Calculate Weight Matrix
            self.layers[layer].propagate(self.synapses[count]);
            count+=1
    
    
